package cn.iocoder.yudao.module.cdk.service.cdklog;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cdk.controller.admin.cdklog.vo.CdklogPageReqVO;
import cn.iocoder.yudao.module.cdk.controller.admin.cdklog.vo.CdklogSaveReqVO;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdklog.CdklogDO;
import cn.iocoder.yudao.module.cdk.dal.mysql.cdklog.CdklogMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cdk.enums.ErrorCodeConstants.LOG_NOT_EXISTS;

/**
 * cdk登录日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CdklogServiceImpl implements CdklogService {

    @Resource
    private CdklogMapper logMapper;

    @Override
    public Integer createlog(CdklogSaveReqVO createReqVO) {
        // 插入
        CdklogDO log = BeanUtils.toBean(createReqVO, CdklogDO.class);
        logMapper.insert(log);
        // 返回
        return log.getId();
    }

    @Override
    public void updatelog(CdklogSaveReqVO updateReqVO) {
        // 校验存在
        validatelogExists(updateReqVO.getId());
        // 更新
        CdklogDO updateObj = BeanUtils.toBean(updateReqVO, CdklogDO.class);
        logMapper.updateById(updateObj);
    }

    @Override
    public void deletelog(Integer id) {
        // 校验存在
        validatelogExists(id);
        // 删除
        logMapper.deleteById(id);
    }

    private void validatelogExists(Integer id) {
        if (logMapper.selectById(id) == null) {
            throw exception(LOG_NOT_EXISTS);
        }
    }

    @Override
    public CdklogDO getlog(Integer id) {
        return logMapper.selectById(id);
    }

    @Override
    public PageResult<CdklogDO> getlogPage(CdklogPageReqVO pageReqVO) {
        return logMapper.selectPage(pageReqVO);
    }

}